package com.snow.auth.controller;

import com.snow.auth.entity.WechatUserEntity;
import com.snow.auth.service.WechatUserService;
import com.snow.conditions.*;
import com.snow.jpa.model.BasePageParams;
import com.snow.jpa.model.IBaseSearchParams;
import com.snow.model.IdParams;
import com.snow.model.PhoneParams;
import com.snow.view.PageData;
import com.snow.view.WebResponse;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/wechat-user")
public class WechatUserController {

    private final WechatUserService wechatUserService;

    public WechatUserController(WechatUserService wechatUserService) {
        this.wechatUserService = wechatUserService;
    }

    @Getter
    @Setter
    @QueryDescription(sort = @Order(value = "createTime", type = QueryOrderType.desc))
    public static class PageParams extends BasePageParams {
        @QueryConditionField(type= ConditionType.like)
        private String name;
        @QueryConditionField
        private String phone;
    }

    @PostMapping("/page")
    public WebResponse<PageData<WechatUserEntity>> page(@RequestBody PageParams params) {
        return WebResponse.success(wechatUserService.page(params));
    }

    @PostMapping("/create")
    public WebResponse<WechatUserEntity> create(@RequestBody WechatUserEntity entity) {
        return WebResponse.success(wechatUserService.save(entity));
    }

    @PostMapping("/link-system-user")
    public WebResponse<Void> linkSystemUser(@RequestBody WechatUserEntity entity) {
        wechatUserService.linkSystemUser(entity);
        return WebResponse.success();
    }

    @PostMapping("/unlink-system-user")
    public WebResponse<Void> unlinkSystemUser(@RequestBody IdParams entity) {
        wechatUserService.unlinkSystemUser(entity.getId());
        return WebResponse.success();
    }
    @PostMapping("/update")
    public WebResponse<WechatUserEntity> update(@RequestBody WechatUserEntity entity) {
        return WebResponse.success(wechatUserService.update(entity));
    }

    @PostMapping("/delete")
    public WebResponse<WechatUserEntity> delete(@RequestBody IdParams params) {
        return WebResponse.success(wechatUserService.delete(params.getId()));
    }

    @Getter
    @Setter
    public static class SearchParams implements IBaseSearchParams {
    }

    @PostMapping("/search")
    public WebResponse<List<WechatUserEntity>> search(@RequestBody SearchParams params) {
        return WebResponse.success(wechatUserService.search(params));
    }
}